<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>订单模块</title>
</head>

<body>
    <h3>上传订单信息</h3>
    <form action="#" method="POST" with="100%">
        订单编号：
        <input name="aid" id="aid" value="" type="text" />
        <br>
        用户编号：
        <input type="text" id="user_id" name="user_id" value=""><br>
        地址编号：
        <input type="text" id="address_id" name="address_id" value=""><br>
        订单状态：
        <input type="text" id="status" name="status" value=""><br>
        订单时间：
        <input type="text" id="order_time" name="order_time" value=""><br>
        支付时间：
        <input type="text" id="pay_time" name="pay_time" value=""><br>
        送达时间：
        <input type="text" id="deliver_time" name="deliver_time" value=""><br>
        签收时间：
        <input type="text" id="received_time" name="received_time" value=""><br>
        <input type="button" onclick="uplodOms()" value="上传" id="sub">
    </form>
    <h3>订单信息：</h3>
    <h3 style="color: red;" id="msg"></h3>
    <br />
    <table border="2px" cellspacing="0px" cellpaddding="10px" bordercolor="pink">
        <thead>
            <tr>
                <td>订单编号</td>
                <td>用户编号</td>
                <td>地址编号</td>
                <td>订单状态</td>
                <td>订单时间</td>
                <td>支付时间</td>
                <td>送达时间</td>
                <td>签收时间</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody id="tbody"></tbody>
    </table>
    <input type="button" onclick="showorder()" value="查询订单详细信息">
    <h3>上传订单详细信息</h3>
    <form action="#" method="POST" with="100%">
        细节编号：
        <input type="text" name="did" id="did" value=""><br>
        订单编号：
        <input type="text" name="order_id" id="order_id" value=""><br>
        产品编号：
        <input type="text" name="product_id" id="product_id" value=""><br>
        商品库存：
        <input type="text" name="count" id="count">件<br>
        <input type="button" value="上传" id="detailsub" onclick="uploaDms()">
    </form>
    <h3>详细订单信息：</h3>
    <h3 style="color: red;" id="dmsg"></h3>
    <table border="2px" bordercolor="pink" cellpadding="10px" cellspacing="0">
        <thead>
            <tr>
                <td>细节编号</td>
                <td>订单编号</td>
                <td>产品编号</td>
                <td>商品库存</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody id="details"></tbody>
    </table>
    <input type="button" value="获取订单详细信息" onclick="showDeatil()">
    <script>
        let xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest;
        } else {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
        function uplodOms() {
            let formdata;
            let msg = document.getElementById('msg');
            let aid = document.getElementById('aid').value;
            let user_id = document.getElementById('user_id').value;
            console.log('user_id: ', user_id);
            let address_id = document.getElementById('address_id').value;
            let status = document.getElementById('status').value;
            let order_time = document.getElementById('order_time').value;
            let pay_time = document.getElementById('pay_time').value;
            let deliver_time = document.getElementById('deliver_time').value;
            let received_time = document.getElementById('received_time').value;
            xmlhttp.onreadystatechange = () => {
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                    msg.innerHTML = JSON.parse(xmlhttp.responseText).msg;
                    // console.log('msg.innerHTML: ', msg.innerHTML);
                }
            }
            xmlhttp.open('POST', "http://127.0.0.1:8080/v1/orders/upload", true);
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            formdata = `aid=${aid}&user_id=${user_id}&address_id=${address_id}&status=${status}&order_time=${order_time}`;
            formdata += `&pay_time=${pay_time}&deliver_time=${deliver_time}&received_time=${received_time}`;
            xmlhttp.send(formdata);
        }

        function uploaDms() {
            let formdata;
            let dmsg = document.getElementById('dmsg');
            let did = document.getElementById('did').value;
            let order_id = document.getElementById('order_id').value;
            let product_id = document.getElementById('product_id').value;
            let count = document.getElementById('count').value;
            xmlhttp.onreadystatechange = () => {
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                    dmsg.innerHTML = JSON.parse(xmlhttp.responseText).msg;
                }
            }
            xmlhttp.open('POST', "http://127.0.0.1:8080/v1/orDetails/upload", true);
            xmlhttp.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
            formdata = `did=${did}&order_id=${order_id}&product_id=${product_id}&count=${count}`;
            xmlhttp.send(formdata);
        }

        function showorder() {
            let html = "";
            let tbody = document.getElementById('tbody');
            xmlhttp.onreadystatechange = () => {
                console.log(11);
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                    let order = JSON.parse(xmlhttp.responseText);
                    console.log('order: ', order);
                    for (let i = 0; i < order.length; i++) {
                        html += `<tr>
                            <td>${order[i].aid}</td>
                            <td>${order[i].user_id}</td>
                            <td>${order[i].address_id}</td>
                            <td>${order[i].status}</td>
                            <td>${order[i].order_time}</td>
                            <td>${order[i].pay_time}</td>
                            <td>${order[i].deliver_time}</td>
                            <td>${order[i].received_time}</td>
                            <td><a href="javascript:deleOr(${order[i].aid})">删除</a></td>
                            </tr>`;
                    }
                    tbody.innerHTML = html;
                }
            };
            xmlhttp.open('GET', 'http://127.0.0.1:8080/v1/orders/list', true);
            xmlhttp.send();
        }

        function showDeatil() {
            let html = "";
            let detail = document.getElementById('details');
            xmlhttp.onreadystatechange = () => {
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                    let detObj = JSON.parse(xmlhttp.responseText);
                    // console.log('detObj: ', detObj);
                    for (let i = 0; i < detObj.length; i++) {
                        html += `<tr>
                            <td>${detObj[i].did}</td>
                            <td>${detObj[i].order_id}</td>
                            <td>${detObj[i].product_id}</td>
                            <td>${detObj[i].count}</td>
                            <td><a href="javascript:deleDe(${detObj[i].did})">删除</a></td>
                            </tr>`;
                    }
                    detail.innerHTML = html;
                }
            }
            xmlhttp.open('GET', 'http://127.0.0.1:8080/v1/orDetails/list2', true);
            xmlhttp.send();
        }

        function deleOr(aid) {
            if(!confirm('是否要删除编号为'+aid+"的信息")){
                return;
            }
            xmlhttp.onreadystatechange = () => {
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                    let msg = JSON.parse(xmlhttp.responseText).code;
                    if (msg == 200) {
                        showorder();
                        alert("删除编号为" + aid + "的信息成功！")
                    } else {
                        alert('删除失败!');
                    }
                }
            }
            let url = "http://127.0.0.1:8080/v1/orders/delete" + aid;
            console.log('url: ', url);
            xmlhttp.open("DELETE", url, true);
            xmlhttp.send();
        }
   
        function deleDe(did){
            let conf=confirm("是否要删除编号为"+did+"信息");
            if(!conf){
                return;
            }
            xmlhttp.onreadystatechange=()=>{
                if(xmlhttp.readyState===4&xmlhttp.status===200){
                let sta=JSON.parse(xmlhttp.responseText).code;
                if(sta==200){
                    showDeatil();
                    alert("删除编号为"+did+"成功！");
                }else{
                    alert('删除失败！');
                }
            }
            }
            let url='http://127.0.0.1:8080/v1/orDetails/delete'+did;
            xmlhttp.open("DELETE",url,true);
            xmlhttp.send();
        }
   </script>
</body>

</html>